const { tool,api } = getApp();
Page({

    data: {
        nickname: '',
        avatar: '',
        gender: '',
        open_id: '',
        stunum: '',     // 学号
        account: '',    // 手机号
        sdept: [],      // 系部列表
        claes: [],      // 班级列表
        sdepObject: {},     // 系部对象
        classObject: {},    // 班级对象
    },

    // 文本框输入
    input(e) {
        let { value } = e.detail;
        let { type } = e.target.dataset;
        if (type * 1) this.setData({ account: value });
        else this.setData({ stunum: value });
    },

    // 选择系部
    sdeptChange(e) {
        let sdepObject = this.data.sdept[e.detail.value];
        tool.getClassBySdept(sdepObject.id).then(claes => {
            this.setData({ sdepObject,claes });
        })
    },

    // 选择班级
    classChange(e) {
        let classObject = this.data.claes[e.detail.value];
        this.setData({ classObject });
    },

    // 注册
    submit() {
        let { stunum,account,classObject,open_id,nickname,avatar,gender } = this.data;
        let msg = '';
        if (!stunum) msg = '请输入学号';
        else if (!account) msg = '请输入手机号';
        else if (!api.checkPhone(account)) msg = '手机号格式有误';
        else if (!classObject.id) msg = '请选择班级';
        if (msg) api.showToast(msg);
        else {
            api.show();
            tool.register({ stunum,account,class_id: classObject.id,open_id,nickname,avatar,gender })
        }
    },

    onLoad(options) {
        let { nickname,avatar,gender } = options;
        this.setData({ nickname,avatar,gender })
        tool.getSdeptList().then(sdept => {
            this.setData({ sdept });
        });
        tool.getOpenId().then(open_id => {
            this.setData({ open_id })
        });
    },

})